package com.lace.security.jwt.util;

import com.lace.security.jwt.auth.UserContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by WangZX on 2018/7/14.
 */
@Component
public class SysUtil implements ApplicationContextAware {

	private static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		if (this.applicationContext == null) {
			this.applicationContext = applicationContext;
		}
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	// 通过name获取Bean.
	public static Object getBean(String name) {
		return getApplicationContext().getBean(name);
	}

	// 通过class获取Bean.
	public static <T> T getBean(Class<T> clazz) {
		return getApplicationContext().getBean(clazz);
	}

	// 通过name,class获取指定的Bean
	public static <T> T getBean(String name, Class<T> clazz) {
		return getApplicationContext().getBean(name, clazz);
	}
	
	public static UserContext getCurrentUser() {
		SecurityContext context = SecurityContextHolder.getContext();
		if(context == null) return null;
		Authentication authentication = context.getAuthentication();
		if(authentication == null) return null;
		Object obj = authentication.getPrincipal();
		if(obj == null) return null;
		if(obj instanceof String) return null;
		return (UserContext)obj;
	}
	
	public static String getCurrentUserName(){
		UserContext user = getCurrentUser();
		if(user == null) return null;
		return user.getUserName();
	}
	
	public static HttpServletRequest getHttpServletRequest(){
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        return request;
	}
	
	public static HttpServletResponse getHttpServletResponse(){
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = attributes.getResponse();
        return response;
	}
	
	public static String getIpAddr() {
		HttpServletRequest request = getHttpServletRequest();
        String ip = request.getHeader("x-forwarded-for");  
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");  
        }  
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    } 
	
}
